package com.ymatou.iapi.shoppingcart.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.shoppingcart.parameter.UpdatePurchaseNumBean;
import com.ymatou.iapi.shoppingcart.service.UpdatePurchaseNumCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.BuyerAppShoppingCartService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.ProductStockInfoWapper;
import com.ymttest.database.mongo.ShoppingCartMongoWapper;
import com.ymttest.utils.EnvSetup;

public class Ts_UpdatePurchaseNum {
	
	private static UpdatePurchaseNumBean updatePurchaseNumBean;
	private static UpdatePurchaseNumCall updatePurchaseNumCall;
	
	private static int sellerId = Integer.parseInt(EnvSetup.getData("zhangyi_seller002_id"));
	private static String sellerName = EnvSetup.getData("zhangyi_seller002_name");
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新购物车商品数量");
	}

	@Before
	public void caseUp() throws Exception {
		updatePurchaseNumBean = new UpdatePurchaseNumBean();
		updatePurchaseNumCall = new UpdatePurchaseNumCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updatePurchaseNumCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_UpdatePurchaseNum_001() {
		 Logger.start(true,"判断接口status是否为200");
		 try {
			 String productId = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			
			 Thread.sleep(60000);
			 
			 BuyerAppShoppingCartService service = new BuyerAppShoppingCartService();
			 service.addShoppingCart(productId, 4085, 1);
			 
			 ShoppingCartMongoWapper wapper = new ShoppingCartMongoWapper();
			 
			 String scId = wapper.getOneShoppingCartInfoForMap("{'productid':'" + productId + "', "
			 		+ "'userid':" + 4085 + "}").get("_id").toString();
			 
			 updatePurchaseNumBean.setPurchaseNum(3);
			 updatePurchaseNumBean.setShoppingCartId(scId);
			 updatePurchaseNumBean.setUserId(4085);
			 
			 updatePurchaseNumCall.setData(updatePurchaseNumBean);
			 updatePurchaseNumCall.callService();
			 
			 int status = updatePurchaseNumCall.getStatus();
			 
			 Logger.comment("updatePurchaseNumCall send:" + updatePurchaseNumCall.getEntityString());
			 Logger.comment("updatePurchaseNumCall response:" + updatePurchaseNumCall.getReturnData());
			 
			 Logger.verifyEquals(200, status, "判断接口status是否为200");		
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_UpdatePurchaseNum_002() {
		 Logger.start(true,"验证result是否为空");
		 try {
			 String productId = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			
			 Thread.sleep(60000);
			 
			 BuyerAppShoppingCartService service = new BuyerAppShoppingCartService();
			 service.addShoppingCart(productId, 4085, 1);
			 
			 ShoppingCartMongoWapper wapper = new ShoppingCartMongoWapper();
			 
			 String scId = wapper.getOneShoppingCartInfoForMap("{'productid':'" + productId + "', "
			 		+ "'userid':" + 4085 + "}").get("_id").toString();
			 
			 updatePurchaseNumBean.setPurchaseNum(3);
			 updatePurchaseNumBean.setShoppingCartId(scId);
			 updatePurchaseNumBean.setUserId(4085);
			 
			 updatePurchaseNumCall.setData(updatePurchaseNumBean);
			 updatePurchaseNumCall.callService();
			 
			 Logger.comment("updatePurchaseNumCall send:" + updatePurchaseNumCall.getEntityString());
			 Logger.comment("updatePurchaseNumCall response:" + updatePurchaseNumCall.getReturnData());
			 
			 Logger.verifyNotNull(updatePurchaseNumCall.getResult(), "验证result是否为空");	
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_UpdatePurchaseNum_003() {
		 Logger.start(true,"验证更新的数量是否和mongodb一致");
		 try {
			 String productId = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			
			 Thread.sleep(60000);
			 
			 BuyerAppShoppingCartService service = new BuyerAppShoppingCartService();
			 service.addShoppingCart(productId, 4085, 1);
			 
			 ShoppingCartMongoWapper wapper = new ShoppingCartMongoWapper();
			 
			 String scId = wapper.getOneShoppingCartInfoForMap("{'productid':'" + productId + "', "
			 		+ "'userid':" + 4085 + "}").get("_id").toString();
			 
			 updatePurchaseNumBean.setPurchaseNum(3);
			 updatePurchaseNumBean.setShoppingCartId(scId);
			 updatePurchaseNumBean.setUserId(4085);
			 
			 updatePurchaseNumCall.setData(updatePurchaseNumBean);
			 updatePurchaseNumCall.callService();
			 
			 int num = Integer.valueOf(wapper.getOneShoppingCartInfoForMap("{'productid':'" + productId + "', "
				 		+ "'userid':" + 4085 + "}").get("num").toString());
			 
			 Logger.comment("updatePurchaseNumCall send:" + updatePurchaseNumCall.getEntityString());
			 Logger.comment("updatePurchaseNumCall response:" + updatePurchaseNumCall.getReturnData());
			 
			 Logger.verifyEquals(num, updatePurchaseNumCall.getResult().getInt("purchaseNum"), "验证更新的数量是否和mongodb一致");	
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_UpdatePurchaseNum_004() {
		 Logger.start(true,"验证更新的数量超出商品库存限制");
		 try {
			 String productId = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			
			 Thread.sleep(60000);
			 
			 BuyerAppShoppingCartService service = new BuyerAppShoppingCartService();
			 service.addShoppingCart(productId, 4085, 1);
			 
			 ShoppingCartMongoWapper wapper = new ShoppingCartMongoWapper();
			 
			 String scId = wapper.getOneShoppingCartInfoForMap("{'productid':'" + productId + "', "
			 		+ "'userid':" + 4085 + "}").get("_id").toString();
			 
			 ProductStockInfoWapper productStockInfoWapper = new ProductStockInfoWapper();
			 int StockNum = Integer.parseInt(productStockInfoWapper.getByProductId(productId).get("StockNum").toString());
			 
			 updatePurchaseNumBean.setPurchaseNum(StockNum + 1);
			 updatePurchaseNumBean.setShoppingCartId(scId);
			 updatePurchaseNumBean.setUserId(4085);
			 
			 updatePurchaseNumCall.setData(updatePurchaseNumBean);
			 updatePurchaseNumCall.callService();
			 
			 Logger.comment("updatePurchaseNumCall send:" + updatePurchaseNumCall.getEntityString());
			 Logger.comment("updatePurchaseNumCall response:" + updatePurchaseNumCall.getReturnData());
			 
			 Logger.verifyEquals("此规格最多可购买300件", updatePurchaseNumCall.getResult().getString("msg"), "验证更新购物车商品规格超库存提醒");
			 Logger.verifyEquals(StockNum, updatePurchaseNumCall.getResult().getInt("purchaseNum"), "验证更新购物车商品规格超库存可添加的最大数量");	
			 Logger.verifyEquals(true, updatePurchaseNumCall.getResult().getBoolean("isMax"), "验证更新购物车商品规格超库存isMax");
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
}
